import { useStore } from '@/hooks';
// eslint-disable-next-line import/no-cycle
import { orderModel } from './stores/definitions';
import { Form, Input, ResponsiveGrid } from '@alifd/next';
import { t } from '@lingui/macro';
import { observer } from 'mobx-react-lite';
import React from 'react';

const { Cell } = ResponsiveGrid;

const CarrierPriceInput = (props) => {
  const { formData, onChange } = props;
  const { orderChangeStore: store } = useStore();

  return (
    <>
      <Form value={formData} onChange={onChange}>
        <ResponsiveGrid gap={[0, 24]} columns={4}>
          <Cell colSpan={2} >
            <Form.Item
              name="carrierPrice"
              label={orderModel['carrierPrice'].label}
              validator={(_, v) => {
                const curPrice = toJS(store.changedPrice);
                // 自定义数字校验规则
                const numberRegex = /^[0-9]+$/; // 匹配数字的正则表达式
                if (!v || v.match(numberRegex)) {
                  if (v <= curPrice) {
                    return Promise.resolve();
                  } else {
                    return Promise.reject(t`公司费用不得超过线路总价！`);
                  }
                } else {
                  return Promise.reject(t`请输入有效的数字！`);
                }
              }}
              style={{ width: '100%' }}
            >
              <Input
                addonTextAfter={t`建议价格:${toJS(formData?.suggestCarrierPrice)}`}
              />
            </Form.Item >
          </Cell>
          <Cell colSpan={2} >
            <Form.Item
              name="driverServiceFee"
              label={orderModel['driverServiceFee'].label}
              style={{ width: '100%' }}
              validator={(_, value) => {
                // 自定义数字校验规则
                const numberRegex = /^[0-9]+$/; // 匹配数字的正则表达式
                if (!value || value.match(numberRegex)) {
                  // 公司费用
                  const companyMoney = Number(formData.carrierPrice);
                  if (value <= companyMoney) {
                    return Promise.resolve();
                  } else {
                    return Promise.reject(t`司机费用不得超过公司费用!!!`);
                  }
                }
                return Promise.reject(t`请输入有效的数字！`);
              }}
            >
              <Input />
            </Form.Item>
          </Cell>
        </ResponsiveGrid>
      </Form >
    </>
  );
};

export default observer(CarrierPriceInput);
